package www.dianbaozu.com.ui.manage

import android.view.View
import androidx.annotation.NonNull
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import dagger.hilt.android.AndroidEntryPoint
import www.dianbaozu.com.R
import www.dianbaozu.com.databinding.FragmentInvitationRecordBinding
import www.wen.com.common.lib.base.fragment.BaseListFragment
import www.wen.com.common.lib.base.module.BaseViewModel
import www.wen.com.common.lib.data.entities.InvitationEntity
import www.wen.com.common.lib.helper.loading.KeyHelper.BUNDLE_DATA
import www.wen.com.common.lib.helper.loading.TitleBarUniversalAdapter

/**
 * @author WEN
 * @Description: 邀请记录
 * @date 2020//25 17:48
 */
@AndroidEntryPoint
class InvitationRecordFragment :
        BaseListFragment<FragmentInvitationRecordBinding, InvitationEntity, InvitationEntity.Content>(),
        View.OnClickListener {

    private lateinit var mAdapter: InvitationRecordAdapter

    private val mInvitationRecordViewModel: InvitationRecordViewModel by viewModels()

    override val viewModel: BaseViewModel
        get() = mInvitationRecordViewModel

    override val mRecyclerView: RecyclerView
        get() = dataBinding.rvInvitationRecords

    override val mSmartRefreshLayout: SmartRefreshLayout
        get() = dataBinding.smartRefreshLayout

    override fun provideAdapter(): BaseQuickAdapter<InvitationEntity.Content, BaseViewHolder> =
            InvitationRecordAdapter().apply {
                mAdapter = this
            }

    override fun isEnableLoadingHelper(): Boolean = true

    override fun getTitleBarUniversalAdapter(): TitleBarUniversalAdapter = TitleBarUniversalAdapter(requireActivity(), tilte = "邀请", action = ::titleAction)

    override fun getLayoutId(): Int = R.layout.fragment_invitation_record

    override fun lazyInitView() {
        super.lazyInitView()
        observeListen()
        launchRequest(false)
    }

    private fun observeListen() {
        mInvitationRecordViewModel.uiState.observe(this) {
            if (it.showSuccess != null) {
                if (it.isRefresh) {
                    mAdapter.setList(it.showSuccess)
                } else {
                    mAdapter.addData(it.showSuccess!!)
                }
            }
        }
    }

    override fun onRefresh(@NonNull refreshLayout: RefreshLayout) {
        launchRequest(true)
    }

    override fun onLoadMore() {
        launchRequest(false)
    }

    private fun launchRequest(isRefresh: Boolean) {
        mInvitationRecordViewModel.requestGeneralListData(requiredParameter = requireArguments().getString(BUNDLE_DATA)!!, isRefresh = isRefresh)
    }

    override fun onReload() {
        launchRequest(false)
    }

}



